package com.view.cn.mvprxjava.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.view.cn.mvprxjava.R;
import com.view.cn.mvprxjava.adapter.ListRcvAdapter;
import com.view.cn.mvprxjava.fragment.socket.ClientSocketFragment;
import com.view.cn.mvprxjava.fragment.socket.ServiceSocketFragment;
import com.view.cn.mvprxjava.utils.FragmentUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 */
public class ListFragment extends BaseFragment {

	public static final String TAG = ListFragment.class.getSimpleName();

	@Bind(R.id.rcv)
	RecyclerView rcv;

	private ListRcvAdapter listRcvAdapter;
	private LinearLayoutManager mLinearLayoutManager;
	private List<Object> list;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	public static ListFragment newInstance() {
		ListFragment fragment = new ListFragment();
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_list, container, false);

		initView(view);
		initData();
		initRcv();
		initEvent();

		return view;
	}

	private void initView(View view) {
		ButterKnife.bind(this, view);
	}

	private void initData() {
		list = new ArrayList<>();
		list.add("用户信息相关");
		list.add("lambda表达式");
		list.add("RxjavaTimerHandler对比");
		list.add("Rxjava");
		list.add("自定义MyView");
		list.add("自定义PieView");
		list.add("View学习Path");
		list.add("自定义view：蜘蛛网雷达");
		list.add("自定义veiw：九宫格解锁");
		list.add("自定义view：时钟");
		list.add("自定义View：五子棋游戏");
		list.add("自定义View：五子棋游戏,人机对战");
		list.add("自定义View：打飞机游戏");
		list.add("红包算法：最大红包金额不能大于最小金额3倍");
		list.add("socket:服务端");
		list.add("socket:客户端");
	}

	private void initRcv() {
		mLinearLayoutManager = new LinearLayoutManager(getActivity());
		mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
		rcv.setLayoutManager(mLinearLayoutManager);
		listRcvAdapter = new ListRcvAdapter(this, list);
		rcv.setAdapter(listRcvAdapter);
	}

	private void initEvent() {
		listRcvAdapter.setOnItemClickListener((view, position) -> {
			if (listRcvAdapter.getEdit()) {
				listRcvAdapter.setEdit(false);
			} else {
				switch (position) {
					case 0:
						FragmentUtils.replaceFragment(mMainActivity, UserFragment.newInstance(), UserFragment.TAG);
						break;
					case 1:
						FragmentUtils.replaceFragment(mMainActivity, RetrolambdaFragment.newInstance(), RetrolambdaFragment.TAG);
						break;
					case 2:
						FragmentUtils.replaceFragment(mMainActivity, RxjavaTimerFragment.newInstance(), RxjavaTimerFragment.TAG);
						break;
					case 3:
						FragmentUtils.replaceFragment(mMainActivity, RxjavaFragment.newInstance(), RxjavaFragment.TAG);
						break;
					case 4:
						FragmentUtils.replaceFragment(mMainActivity, MyViewFragment.newInstance(), MyViewFragment.TAG);
						break;
					case 5:
						FragmentUtils.replaceFragment(mMainActivity, PieViewFragment.newInstance(), PieViewFragment.TAG);
						break;
					case 6:
						FragmentUtils.replaceFragment(mMainActivity, PathViewFragment.newInstance(), PathViewFragment.TAG);
						break;
					case 7:
						FragmentUtils.replaceFragment(mMainActivity, RadarViewFragment.newInstance(), RadarViewFragment.TAG);
						break;
					case 8:
						FragmentUtils.replaceFragment(mMainActivity, LockViewFragment.newInstance(), LockViewFragment.TAG);
						break;
					case 9:
						FragmentUtils.replaceFragment(mMainActivity, WatchBoardFragment.newInstance(), WatchBoardFragment.TAG);
						break;
					case 10:
						FragmentUtils.replaceFragment(mMainActivity, BackgammonViewFragment.newInstance(), BackgammonViewFragment.TAG);
						break;
					case 11:
						FragmentUtils.replaceFragment(mMainActivity, BackgammonViewFragment2.newInstance(), BackgammonViewFragment2.TAG);
						break;
					case 12:
						FragmentUtils.replaceFragment(mMainActivity, GameFeiJiFragment.newInstance(), GameFeiJiFragment.TAG);
						break;
					case 13:
						FragmentUtils.replaceFragment(mMainActivity, MoneyFragment.newInstance(), MoneyFragment.TAG);
					case 14:
						FragmentUtils.replaceFragment(mMainActivity, ServiceSocketFragment.newInstance(), ServiceSocketFragment.TAG);
						break;
					case 15:
						FragmentUtils.replaceFragment(mMainActivity, ClientSocketFragment.newInstance(), ClientSocketFragment.TAG);
					default:
						break;
				}
			}
		});
		listRcvAdapter.setOnItemLongClickListener(v -> {
			listRcvAdapter.setEdit(true);
			return false;
		});
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		ButterKnife.unbind(this);
	}
}
